package com.yj.ccs.abs.oa.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.yj.ccs.abs.common.model.OaResponse;
import com.yj.ccs.abs.oa.entity.OaAsset;
import com.yj.ccs.abs.oa.service.IntegrationService;
import com.yj.ccs.abs.util.gson.LocalDateAdapter;
import com.yj.ccs.abs.util.gson.LocalDateTimeAdapter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/19
 * Author     : Unbelievable9
 **/
@Api(tags = "OA-Integration-Controller")
@RestController
@RequestMapping("/oa")
public class IntegrationController {

    private static final Logger logger = LoggerFactory.getLogger(IntegrationController.class);

    private Gson gson;

    private final IntegrationService integrationService;

    @Autowired
    public IntegrationController(IntegrationService integrationService) {
        this.integrationService = integrationService;
    }

    @PostConstruct
    public void initGson() {
        gson = new GsonBuilder()
                .setPrettyPrinting()
                .registerTypeAdapter(LocalDate.class, new LocalDateAdapter())
                .registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter())
                .create();
    }

    @ApiOperation("推送资产信息")
    @PostMapping(value = "/push-asset", consumes = MediaType.TEXT_HTML_VALUE, produces = MediaType.TEXT_HTML_VALUE)
    public void pushAsset(@RequestBody String assetString, HttpServletResponse response) throws IOException {
        Type assetType = new TypeToken<OaAsset>() {
        }.getType();
        OaResponse<Object> oaResponse;

        try {
            String assetJson = new String(Base64Utils.decode(assetString.getBytes()), "UTF-8");

            logger.debug("OA资产信息推送内容:\n" + assetJson);

            OaAsset oaAsset = gson.fromJson(assetJson, assetType);

            Boolean success = integrationService.createAsset(oaAsset);

            if (success) {
                oaResponse = new OaResponse<>(1, "资产信息推送成功", null);
            } else {
                oaResponse = new OaResponse<>(1, "资产信息推送失败, 债务人不存在", null);
            }
        } catch (Exception e) {
            logger.warn("OA Push Error:\n", e);

            oaResponse = new OaResponse<>(2, "资产信息推送失败", null);
        }

        response.getOutputStream().write(Base64Utils.encode(gson.toJson(oaResponse).getBytes("UTF-8")));
    }
}
